package cn.b.training.baby_education_training.service.impl;

import cn.b.training.baby_education_training.entity.BabyInfo;
import cn.b.training.baby_education_training.entity.UserInfo;
import cn.b.training.baby_education_training.repository.BabyInfoRepository;
import cn.b.training.baby_education_training.service.BabyInfoService;
import cn.b.training.baby_education_training.service.UserInfoService;
import cn.b.training.baby_education_training.service.dto.BabyDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.List;

/**
 * @program: baby_education_training
 * @description
 * @author: wuying
 * @create: 2022-12-01 01:03
 **/
@Service
public class BabyInfoServiceImpl implements BabyInfoService {

    private final BabyInfoRepository babyInfoRepository;
    private final UserInfoService userInfoService;
    @Autowired
    private BaiduAiService baiduAiService;

    public BabyInfoServiceImpl(BabyInfoRepository babyInfoRepository, UserInfoService userInfoService) {
        this.babyInfoRepository = babyInfoRepository;
        this.userInfoService = userInfoService;
    }

    //@Override
    //public BabyInfo insertBaby(String babyName, String babyGender, String babyAvatar) throws Exception {
    //    BabyInfo babyInfo = new BabyInfo();
    //    babyInfo.setBabyName(babyName);
    //    babyInfo.setBabyGender(babyGender);
    //    babyInfo.setBabyAvatar(babyAvatar);
    //    babyInfo.setBabyBirth(Timestamp.valueOf(LocalDateTime.now()));
    //    return babyInfo;
    //}

    @Override
    public boolean insertBaby(BabyDto babyDto) throws Exception {
        String phone = ((User) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUsername();
        UserInfo account = userInfoService.getByPhone(phone);
        BabyInfo babyInfo = new BabyInfo();
        //if (babyDto.getBabyAvatar() != null) {
        //    String fileKey = UUID.randomUUID().toString();
        //    String fileName = babyDto.getBabyAvatar().getOriginalFilename();
        //    if (fileName.indexOf(".") > 0) {
        //        String extName = fileName.substring(fileName.lastIndexOf(".") - 1);
        //        fileKey += extName;
        //    }
        //    try {
        //        baiduAiService.putFile(fileKey, babyDto.getBabyAvatar().getInputStream());
        //    } catch (Exception e) {
        //        e.printStackTrace();
        //    }
        //    babyInfo.setBabyAvatar("http://hzw-bos.bj.bcebos.com/baby/" + fileKey);
        //}
        babyInfo.setBabyAvatar(babyDto.getBabyAvatar());
        babyInfo.setUserId(account.getId());

            babyInfo.setBabyBirth(babyDto.getBabyBirth());
        babyInfo.setBabyName(babyDto.getBabyName());
        babyInfo.setBabyGender(babyDto.getBabyGender());
        babyInfo = babyInfoRepository.save(babyInfo);
        if (babyInfo!=null){
            return true;
        }
        return false;
    }

    @Override
    public BabyInfo getBabyById(Long id) {
        return babyInfoRepository.getByBabyId(id);
    }

    @Override
    public boolean updateBabyInfo(BabyDto babyDto) throws ParseException {
        String phone = ((User) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUsername();
        UserInfo account = userInfoService.getByPhone(phone);

        BabyInfo babyInfo = babyInfoRepository.getByBabyId(babyDto.getId());
        //if (babyDto.getBabyAvatar() != null) {
        //    String fileKey = UUID.randomUUID().toString();
        //    String fileName = babyDto.getBabyAvatar().getOriginalFilename();
        //    if (fileName.indexOf(".") > 0) {
        //        String extName = fileName.substring(fileName.lastIndexOf(".") - 1);
        //        fileKey += extName;
        //    }
        //    try {
        //        baiduAiService.putFile(fileKey, babyDto.getBabyAvatar().getInputStream());
        //    } catch (Exception e) {
        //        e.printStackTrace();
        //    }
            babyInfo.setBabyAvatar(babyDto.getBabyAvatar());
        //}
            babyInfo.setBabyBirth(babyDto.getBabyBirth());
        babyInfo.setBabyName(babyDto.getBabyName());
        babyInfo.setBabyGender(babyDto.getBabyGender());
        babyInfoRepository.save(babyInfo);
        if (babyInfo!=null){
            return true;
        }
        return false;
    }

    @Override
    public boolean deleteBaby(Long id) {
        try{
            this.babyInfoRepository.deleteById(id);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public List<BabyInfo> getBabyByUserId(Long userId) {
        List<BabyInfo> babyInfos = babyInfoRepository.getBabyByUserId(userId);
        return babyInfos;
    }


}
